#include "decodethread.h"
#include <QDebug>
#include <QImage>
#include <QFile>
#include "cqrcoder.h"
#include <errno.h>
#include "qandroidmultimediautils.h"

#ifdef S1
#include "gralloc_priv.h"
#include <gst/egl/egl.h>
#include <hybris/eglplatformcommon/hybris_nativebufferext.h>
#include <hybris/eglplatformcommon/nativewindowbase.h>
#endif

#define  BARCODE_IMAGE_WIDTH 800
//#define  BARCODE_IMAGE_HEIGHT 800
#define  DECODE_TMP_IMAGE_PATH "/tmp/syberosDecodeImage.jpg"

DecodeThread::DecodeThread(QObject *parent): QObject(parent), m_nDecode(true)
{
    qDebug() <<Q_FUNC_INFO<<", DecodeThread create";
}

DecodeThread::~DecodeThread()
{
    qDebug() <<Q_FUNC_INFO<<", DecodeThread destruct start";
    workThread.quit();
    workThread.wait();
    qDebug() <<Q_FUNC_INFO<<", DecodeThread destruct end";
}

void DecodeThread::decodeBegin(const QVideoFrame &frame)
{
    qDebug()<<Q_FUNC_INFO<<workThread.isRunning()<< "," << workThread.isFinished() << "," << QThread::currentThreadId();
    if(!workThread.isRunning()){
        this->moveToThread(&workThread);
        connect(this, SIGNAL(decodeBeginSignal()), this, SLOT(decodeFrame()));
        workThread.start();
    }

    if(frame.isValid()){
        QVideoFrame clonedFrame(frame);
        clonedFrame.map(QAbstractVideoBuffer::ReadOnly);
        mVFrame.copyFromQVFrame(clonedFrame);
        clonedFrame.unmap();
        qDebug() << Q_FUNC_INFO << "0003";
        emit decodeBeginSignal();
    }
}

void DecodeThread::decodeFrame()
{
    CVFrame &frame = mVFrame;
    qDebug() << Q_FUNC_INFO << ",pformat:" << frame.pixelFormat << ",tid:" << QThread::currentThreadId();
    const uchar *bitsSrc = frame.pBuf;
    if(!bitsSrc){
        qDebug() << Q_FUNC_INFO << ", bitSrc is null";
        return;
    }
#ifdef S1
    qDebug() << Q_FUNC_INFO << ",this is sl";
    GstEGLImageMemory *mem = (GstEGLImageMemory *) bitsSrc;
    ANativeWindowBuffer *nativeWindowBuffer = (ANativeWindowBuffer*)(mem->client_buffer);
    private_handle_t *handle = (private_handle_t *)nativeWindowBuffer->handle;

    QImage orgImg = QImage(frame.width, frame.height, QImage::Format_ARGB32);
    qt_convert_NV21_to_ARGB32((const uchar*)handle->base,
                              (quint32 *)orgImg.bits(),
                              frame.width,
                              frame.height);
#else
    QImage orgImg = QImage(frame.width, frame.height, QImage::Format_ARGB32);
    qt_convert_NV21_to_ARGB32(bitsSrc,
                              (quint32 *)orgImg.bits(),
                              frame.width,
                              frame.height);
#endif

    QMatrix matrix;
    matrix.rotate(90);
    orgImg = orgImg.transformed(matrix);

    QFile::remove(DECODE_TMP_IMAGE_PATH);
    QImage img = orgImg.copy((orgImg.width() - BARCODE_IMAGE_WIDTH)/2,
                             (orgImg.height() - BARCODE_IMAGE_WIDTH)/2,
                             BARCODE_IMAGE_WIDTH, BARCODE_IMAGE_WIDTH);
    bool saveRv = img.save(DECODE_TMP_IMAGE_PATH);
    qDebug() <<"save imag rlt:" <<saveRv;
    QString content;
    int type;
    qDebug() << "decode pic start" << DECODE_TMP_IMAGE_PATH;
    int rv = CQRCoder::decodeQRFunc(DECODE_TMP_IMAGE_PATH, content, type);
    qDebug() << "decode pic end, rlt:" << rv << ",type is:" << type <<",tid:" << QThread::currentThreadId();
    emit decodeFinished(DECODE_TMP_IMAGE_PATH, content, rv);
}

